package com.agrishare.repository;

import com.agrishare.entity.PointRecord;
import com.agrishare.entity.PointType;
import com.agrishare.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import java.time.LocalDateTime;

@Repository
public interface PointRecordRepository extends JpaRepository<PointRecord, Long> {
    boolean existsByUserAndTypeAndRelatedIdAndCreatedAtAfter(
        User user, PointType type, Long relatedId, LocalDateTime date);

    Page<PointRecord> findByUser(User user, Pageable pageable);

    @Query("SELECT SUM(pr.points) FROM PointRecord pr WHERE pr.user = :user")
    Integer getTotalPointsByUser(@Param("user") User user);
}